package org.idea.qiyu.common.utils;


import com.alibaba.fastjson.JSON;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.idea.qiyu.common.dto.AgentCollectorDataDTO;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * apm上报信息工具
 *
 * @Author linhao
 * @Date created in 7:34 下午 2021/9/9
 */
public class ApmAgentUtil {

    public static final String APM_AGENT_ADDRESS="http://localhost:8772/agent/report";

    public static boolean doReport(AgentCollectorDataDTO agentCollectorDataDTO) throws IOException {
        if(agentCollectorDataDTO==null){
            return false;
        }
        List<NameValuePair> nameValuePairList = new ArrayList<>(1);
        String jsonParam = JSON.toJSONString(agentCollectorDataDTO);
        nameValuePairList.add(new BasicNameValuePair("jsonInput",jsonParam));
        HttpClientUtils.doPostForm(APM_AGENT_ADDRESS,null,nameValuePairList);
        return true;
    }


    public static void main(String[] args) throws IOException {
        AgentCollectorDataDTO agentCollectorDataDTO = new AgentCollectorDataDTO();
        agentCollectorDataDTO.setTimestamp(System.currentTimeMillis());
        agentCollectorDataDTO.setReportType((short)2);
        agentCollectorDataDTO.setJson("this is a test input");
        boolean status = ApmAgentUtil.doReport(agentCollectorDataDTO);
        System.out.println(status);
    }

}
